(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('notifyTplService', notifyTplService);
	notifyTplService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory' ];
	function notifyTplService($q, loggerFactory, commonService, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("notifyTplService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getNotifyTypes : {
				url : 'do/cda/dataList/getNotifyTypeOption',
				method : 'post'
			},
			getNotifyPartys : {
				url : 'do/cda/dataList/getNotifyPartyOption',
				method : 'post'
			},
			getLocales : {
				url : 'do/cda/dataList/locales',
				method : 'post'
			},
			getYesNo : {
				url : 'do/cda/dataList/yesno',
				method : 'get'
			},
		});
		function getYesNoOptions() {
			var defer = $q.defer();
			ajaxService.getYesNo().then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		function getNotifyTplMimeTypeOptions(params) {
			var data =  [{
				"value" : "",
				"label" : ""
			}, {
				"value" : "text/plain",
				"label" : "label.mime.textplain"
			}, {
				"value" : "text/html",
				"label" : "label.mime.texthtml"
			}];
			return data;
		}
		function getNotifyTypes(notifyTpl) {
			var defer = $q.defer();
			ajaxService.getNotifyTypes(notifyTpl).then(function(response) {
				response.callback = {"changed" : "vm.notifyTypeChanged"};
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getLocales() {
			var defer = $q.defer();
			ajaxService.getLocales().then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		function getNotifyPartys(notifyTpl) {
			var defer = $q.defer();
			ajaxService.getNotifyPartys(notifyTpl).then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getNotifyTypeOptions(ctrl) {
			ctrl.notifyTypeOptions ={};
			if(ctrl.notifyTpl.notifyTypes){
				getNotifyTypes(ctrl.notifyTpl).then(function(response){
					angular.merge(ctrl.notifyTypeOptions, response);
				});
			}
		}
		function getNotifyPartyAndNotifyTypeOptions(ctrl) {
			ctrl.notifyPartyOptions ={};
			ctrl.notifyTypeOptions ={};
			if(ctrl.notifyTpl.eventCode){
				getNotifyPartys(ctrl.notifyTpl).then(function(response){
					angular.merge(ctrl.notifyPartyOptions, response.data);
				});
				getNotifyTypes(ctrl.notifyTpl).then(function(response){
					angular.merge(ctrl.notifyTypeOptions, response.data);
				});
			}
			ctrl.eventCodeChanged = function(selectdeRows){
				if(!ctrl.notifyTpl.eventCode) {
					ctrl.notifyPartyOptions = {};
					ctrl.notifyTypeOptions = {};
				}
			};
		}
		return {
			getNotifyTplMimeTypeOptions : getNotifyTplMimeTypeOptions,
			getNotifyPartyAndNotifyTypeOptions : getNotifyPartyAndNotifyTypeOptions,
			getLocales : getLocales,
			getYesNoOptions : getYesNoOptions
		};
	}
})(angular);
